package com.tulingxueyuan.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tulingxueyuan.order.feignService.StockFeignService;
import com.tulingxueyuan.order.pojo.Order;
import com.tulingxueyuan.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;

/***
 * @Author 徐庶   
 * @Slogan 致敬大师，致敬未来的你
 */
@RestController
@RequestMapping("/order")
@RefreshScope// 配置中心动态刷新
public class OrderController {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    StockFeignService stockFeignService;
    @Value("${author}")
    String author;
    @Value("${spring.cloud.sentinel.transport.dashboard}")
    String dashboard;

    @Autowired
    OrderService orderService;

    // 插入订单信息
    @RequestMapping("/add")
    public String add(){
        Order order=new Order();
        order.setProductId(9);
        order.setStatus(0);
        order.setTotalAmount(100);

        orderService.create(order);
        return "下单成功";
    }


    /* *
     * @Author: lidong
     * @Date: 2025/3/6 22:22
     * @Description: sentinel测试
     */
    @RequestMapping("/test")
    // 添加Sentinel注解，指定资源名称和限流处理方法
    @SentinelResource(value = "testOrder", blockHandler = "handleTestBlock", fallback = "handleTestFallback")
    public String test() throws InterruptedException {
        Thread.sleep(1000);
        return "下单成功" + dashboard;
    }

    // 限流处理方法，需要和原方法参数一致，额外添加BlockException参数
    public String handleTestBlock(BlockException ex) {
        return "测试失败，触发限流: " + ex.getClass().getSimpleName();
    }

    // 熔断处理方法，需要和原方法参数一致
    public String handleTestFallback() {
        return "测试失败，触发熔断";
    }
}
